<?php

namespace ServerApp\lib\server\model\swoole\lib;

use swoole_table;

class SwooleTable
{
    public $swoole_t = null;
    public $primary_key_set = [];


    public function __construct($size, $columns){
        $this->swoole_t = new swoole_table($size);
        foreach ($columns as $field_name => $detail) {
            $this->swoole_t->column($field_name, $detail[0], $detail[1]);
        }
        $this->swoole_t->create();
    }

    public function getRow($primary_key) {
        return $this->swoole_t->get($primary_key . '');
    }

    public function setRow($primary_key, $value) {
        $primary_key = $primary_key . '';
        $this->primary_key_set[$primary_key] = true;
        return $this->swoole_t->set($primary_key, $value);
    }

    public function isExistTheRow($primary_key) {
        $primary_key = $primary_key . '';
        return isset($this->primary_key_set[$primary_key]);
//        return $this->swoole_t->exist($primary_key . '');
    }

    public function delRow($primary_key) {
        $primary_key = $primary_key . '';
        unset($this->primary_key_set[$primary_key]);
        return $this->swoole_t->del($primary_key);
    }
}